package com.program.facesigninsystem.controller;

import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.program.facesigninsystem.entity.Friend;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.service.IFriendService;
import com.program.facesigninsystem.service.IRedisService;
import com.program.facesigninsystem.util.PageUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @author 盛旭雯
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/friend")
public class FriendController {
    private final static Logger logger = LoggerFactory.getLogger(FriendController.class);

    private IFriendService iFriendService;
    private IRedisService iRedisService;

    @Autowired
    public FriendController(IFriendService iFriendService, IRedisService iRedisService) {
        this.iFriendService = iFriendService;
        this.iRedisService = iRedisService;
    }

    @RequestMapping(value = "/join",method = RequestMethod.GET)
    @CurrentLimiter(QPS=5)
    public ResponseModel joinFriend(@RequestParam("user_id")String user_id,@RequestParam("friend_id")String friend_id){
        Friend friend=iFriendService.selectExist(user_id,friend_id);
        if(friend==null){
            try{
                iFriendService.insertFriend(user_id, friend_id);
                return new ResponseModel(200,"加入成功");
            }catch (Exception e){
                return new ResponseModel(198,"您可能尚未注册，请先注册");
            }
        }else{
            return new ResponseModel(199,"您已与对方互为好友");
        }
    }
    @RequestMapping(value = "/order",method = RequestMethod.GET)
    @CurrentLimiter(QPS=5)
    public ResponseModel SignInOrder(@RequestParam("user_id")String user_id){
        try {
            List<Map<String,Object>> rank=iFriendService.selectByOrder(user_id);
            return new ResponseModel(200,"获取成功",rank);
        }catch (Exception e) {
            return new ResponseModel(200, "获取失败");
        }
    }

    @RequestMapping(value = "/rank",method = RequestMethod.GET)
    public void SignInRank(HttpServletResponse resp,@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize){
        try {
            List<Map<String,Object>> mapList=iRedisService.getRank();
            try {
                JSONObject jsonObject = PageUtil.wrap2(mapList,"Rank",pageSize,pageNum);
                resp.setCharacterEncoding("utf-8");
                /*得到输出流*/
                PrintWriter respWriter = resp.getWriter();
                /*将JSON格式的对象toString()后发送*/
                respWriter.append(jsonObject.toString());
            } catch (Exception e) {
                logger.info(String.valueOf(e));
            }
        }catch (Exception e){
            logger.info(String.valueOf(e));
        }
    }


    @RequestMapping(value = "/scoreRank",method = RequestMethod.GET)
    public void scoreRank(HttpServletResponse resp,@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize){
        try {
            List<Map<String,Object>> mapList=iFriendService.scoreRank();
            try {
                JSONObject jsonObject = PageUtil.wrap2(mapList,"Rank",pageSize,pageNum);
                resp.setCharacterEncoding("utf-8");
                PrintWriter respWriter;
                respWriter = resp.getWriter();
                respWriter.append(jsonObject.toString());
            } catch (Exception e) {
                logger.info(String.valueOf(e));
            }
        }catch (Exception e){
            logger.info(String.valueOf(e));
        }
    }
}
